package opensource.web.util.json;

import java.util.HashMap;

public class JsonO {
	
	private StringBuffer str;
	
	public JsonO(){
		str = new StringBuffer("{}");
	}
	
	public JsonO(HashMap<String, Object> map){
		str = new StringBuffer("{");
		for(String key : map.keySet())
			str.append("\""+key+"\":\""+map.get(key).toString()+"\",");
		str = str.deleteCharAt(str.length()-1);
		str.append("}");
	}
	
	public void put(String key, JsonO jsonO){
		String temp = str.toString();
		str = str.deleteCharAt(str.length()-1);
		if(str.length()!=1) str.append(",");
		str.append("\""+key+"\":"+temp);
		str.append("}");
	}
	
	public void put(String key, String s){
		str = str.deleteCharAt(str.length()-1);
		if(str.length()!=1) str.append(",");
		str.append("\""+key+"\":\""+s+"\"");
		str.append("}");
	}
	
	public String getString(){
		return str.toString();
	}
	
	public static void main(String[] args){
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("name", "zhangyu");
		map.put("age", 21);
		map.put("gender","male");
		
		JsonO jsonO = new JsonO(map);
		JsonA jsonA = new JsonA();
		jsonA.append(jsonO);
		System.out.println(jsonO.getString());
		System.out.println(jsonA.getString());
		System.out.println("-----------------");
		jsonO.put("props", jsonO);
		jsonO.put("bit", "helllo");
		System.out.println(jsonO.getString());
		jsonA.append(jsonO);
		System.out.println(jsonA.getString());
		System.out.println("-----------------");
		
	}
}
